package com.zhuiyun.project.config;

import com.rabbitmq.client.AMQP;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

/**
 * @Description TODO   RabbitMq 配置类
 * @Author shy
 * @Date 2024/3/8 14:47
 */
@Configuration
public class RabbitMqConfig {
    //正常队列
    public static final String LIVE_EXCHANGE="live-exchange";
    //正常交换机
    public static final String LIVE_QUEUE="live-queue";
    //路由
    public static final String LIVE_ROUTING_KEY="liven";
    //死信队列
    public static final String DEAD_EXCHANGE="dead-exchange";
    //死信交换机
    public static final String DEAD_QUEUE="dead-queue";
    //死信队列路由
    public static final String DEAD_ROUTING_KEY="dead";

    /*
        死信交换机
     */
    @Bean
    public Exchange deadExchange() {
        return ExchangeBuilder.directExchange(DEAD_EXCHANGE).build();
    }
    /*
        死信队列
     */
    @Bean
    public Queue deadQueue(){
        return QueueBuilder.durable(DEAD_QUEUE).build();
    }
    /*
    绑定死信的队列和交换机  路由
     */
    @Bean
    public Binding deadBinding(){
        return BindingBuilder.bind(deadQueue()).to(deadExchange()).with(DEAD_ROUTING_KEY).noargs();
    }
    //声明一个正常的交换机
    @Bean
    public Exchange LiveExchange(){
        return ExchangeBuilder.directExchange(LIVE_EXCHANGE).build();
    }

    //将正常队列和死信队列绑定
    @Bean
    public Queue LiveQueue(){
        return QueueBuilder.durable(LIVE_QUEUE)
                .deadLetterExchange(DEAD_EXCHANGE)
                .deadLetterRoutingKey(DEAD_ROUTING_KEY)
                .build();
        //              .ttl(10000)
    }
    //正常队列绑定交换机 路由
    @Bean
    public Binding LiveBinding(){
        return BindingBuilder.bind(LiveQueue()).to(LiveExchange()).with(LIVE_ROUTING_KEY).noargs();
    }

}